﻿using Domain.Event;
using Domain.IRepositorys;
using Domain.Model;
using Domain.Services;
using Domain.Types;
using DTO.Admin;
using Infrastructure.Error;
using Infrastructure.UniOfWorkBase;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin
{
    public class AdminApplication
    {
        private WalletService walletService { get; set; }
        private AdminService adminService { get; set; }
        private IMemoryCache memoryCache { get; set; }
        private IAdminRepository adminRepository { get; set; }
        private IRoleRepository roleRepository { get; set; }
        private IBannerRepository bannerRepository { get; set; }
        private IUserRepository userRepository { get; set; }
        private IWalletRepository walletRepository { get; set; }
        private IWithDrawRepository withDrawRepository { get; set; }
        private IInComeWalletLogRepository inComeWalletLogRepository { get; set; }
        private IChargeWalletLogRepository chargeWalletLogRepository { get; set; }
        private IBlackListRepository blackListRepository { get; set; }
        private IDicRepository dicRepository { get; set; }
        private ISmsCodeRepository smsCodeRepository { get; set; }
        private IFinancialRepository financialRepository { get; set; }
        private IMessageRepository messageRepository { get; set; }
        private IKFQARepository kFQARepository { get; set; }
        private IEFUnitOfWork efUnitOfWork { get; set; }
        public AdminApplication(AdminService adminService, WalletService walletService, IMemoryCache memoryCache, IAdminRepository adminRepository, IRoleRepository roleRepository, IBannerRepository bannerRepository, IUserRepository userRepository, IWalletRepository walletRepository, IInComeWalletLogRepository inComeWalletLogRepository, IChargeWalletLogRepository chargeWalletLogRepository, IBlackListRepository blackListRepository, IEFUnitOfWork efUnitOfWork, IWithDrawRepository withDrawRepository, IDicRepository dicRepository, ISmsCodeRepository smsCodeRepository, IFinancialRepository financialRepository, IMessageRepository messageRepository, IKFQARepository kFQARepository)
        {
            this.walletService = walletService;
            this.adminService = adminService;
            this.memoryCache = memoryCache;
            this.adminRepository = adminRepository;
            this.roleRepository = roleRepository;
            this.bannerRepository = bannerRepository;
            this.userRepository = userRepository;
            this.walletRepository = walletRepository;
            this.inComeWalletLogRepository = inComeWalletLogRepository;
            this.chargeWalletLogRepository = chargeWalletLogRepository;
            this.blackListRepository = blackListRepository;
            this.efUnitOfWork = efUnitOfWork;
            this.withDrawRepository = withDrawRepository;
            this.dicRepository = dicRepository;
            this.smsCodeRepository = smsCodeRepository;
            this.financialRepository = financialRepository;
            this.messageRepository = messageRepository;
            this.kFQARepository = kFQARepository;
        }

        public AdminLoginOutput Login(AdminLoginInput adminLoginInput)
        {
            var token = adminService.Login(adminLoginInput.AdminName, adminLoginInput.AdminPwd);

            AdminLoginOutput adminLoginOutput = new AdminLoginOutput();

            adminLoginOutput.Token = token;
            adminLoginOutput.Expire = 60 * 60 * 24;
            return adminLoginOutput;
        }

        #region 权限验证

        public bool IsLoginTimeOut(Dictionary<string, object> payLoad)
        {
            var time = DateTime.Parse(payLoad["time"].ToString());
            var expires = int.Parse(payLoad["expires"].ToString());

            if (time.AddSeconds(expires) <= DateTime.Now)
            {
                return true;
            }
            return false;
        }

        public ISet<string> GetRolePerssion(int roleID)
        {
            RoleAggregate role = roleRepository.Entities.FirstOrDefault(a => a.ID == roleID);
            ISet<string> set = new HashSet<string>();
            if (!string.IsNullOrWhiteSpace(role.RolePermissions))
            {
                foreach (var item in role.RolePermissions.Split(','))
                {
                    set.Add(item);
                }
            }
            return set;
        }

        public ISet<string> GetPressions(int adminId, bool useCache = true)
        {
            string key = "AdminService.GetPressions." + adminId;
            if (useCache)
            {
                var temp = memoryCache.Get<ISet<string>>(key);
                if (temp != null) return temp;
            }

            var adminRole = adminRepository.Entities.First(a => a.ID == adminId);
            StringBuilder stringBuilder = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(adminRole.Roles))
            {
                var arr = adminRole.Roles.Split(',').Select(i => int.Parse(i));
                foreach (var roleID in arr)
                {
                    RoleAggregate role = roleRepository.Entities.First(i => i.ID == roleID);
                    if (role != null && role.Status == 0)
                    {
                        stringBuilder.Append(role.RolePermissions);
                        if (!role.RolePermissions.EndsWith(',')) stringBuilder.Append(",");
                    }
                }
            }

            var ps = stringBuilder.ToString().TrimEnd(',').Split(',');
            ISet<string> set = new HashSet<string>();
            foreach (var item in ps)
            {
                set.Add(item);
            }

            memoryCache.Set<ISet<string>>(key, set, new DateTimeOffset(DateTime.Now.AddHours(2)));

            return set;
        }


        #endregion

        #region 获取菜单列表

        public ISet<string> GetMenus(Dictionary<string, object> payLoad)
        {
            var adminId = int.Parse(payLoad["adminId"].ToString());
            ISet<string> adminPressionSet = null;
            if (adminId == 1)
            {
                adminPressionSet = new HashSet<string>();
                adminPressionSet.Add("*");
            }
            else
            {
                adminPressionSet = GetPressions(adminId, false);
            }
            return adminPressionSet;
        }

        #endregion

        #region 管理员 

        public List<AdminAggregate> GetAdminList(GetAdminListReq req, out int count)
        {
            if (!string.IsNullOrEmpty(req.AdminName))
            {
                count = adminRepository.Entities.Where(a => a.AdminName.Contains(req.AdminName) && a.IsDelete == false).Count();
                return adminRepository.Entities.Where(a => a.AdminName.Contains(req.AdminName) && a.IsDelete == false).OrderByDescending(a => a.ID).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = adminRepository.Entities.Where(a => a.IsDelete == false).Count();
                return adminRepository.Entities.Where(a => a.IsDelete == false).OrderByDescending(a => a.ID).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }

        public bool AddAdmin(AdminInput admin)
        {
            var info = adminRepository.Entities.FirstOrDefault(a => a.AdminName == admin.AdminName);
            if (info != null)
            {
                throw new BizException("用户名重复!");
            }

            AdminAggregate adminAggregate = new AdminAggregate();
            adminAggregate.AdminName = admin.AdminName;
            adminAggregate.AdminPass = admin.AdminPass;
            adminAggregate.IsDelete = false;
            adminAggregate.Roles = admin.Roles;
            adminAggregate.CT = DateTime.Now;
            adminAggregate.UT = DateTime.Now;

            adminRepository.Insert(adminAggregate);

            return efUnitOfWork.Commit() > 0;
        }

        public bool EditAdmin(AdminInput admin)
        {
            AdminAggregate adminAggregate = new AdminAggregate();
            adminAggregate.ID = admin.Id;
            adminAggregate.AdminName = admin.AdminName;
            adminAggregate.AdminPass = admin.AdminPass;
            adminAggregate.IsDelete = admin.IsDelete < 1;
            adminAggregate.Roles = admin.Roles;
            adminAggregate.UT = DateTime.Now;

            adminRepository.Update(adminAggregate);

            return efUnitOfWork.Commit() > 0;
        }

        public bool DelAdmin(int id)
        {
            var info = adminRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (info == null) return false;
            info.IsDelete = true;
            adminRepository.Update(info);
            return efUnitOfWork.Commit() > 0;
        }


        #endregion

        #region 角色 


        public RoleAggregate GetRole(int roleId)
        {
            return roleRepository.Entities.FirstOrDefault(a => a.ID == roleId && a.IsDelete == false);
        }

        public List<RoleAggregate> GetRoleList(GetRoleListReq req, out int count)
        {
            if (req.IsAll == 0)
            {
                if (!string.IsNullOrEmpty(req.RoleName))
                {
                    count = roleRepository.Entities.Where(a => a.Name.Contains(req.RoleName) && a.IsDelete == false).Count();
                    return roleRepository.Entities.Where(a => a.Name.Contains(req.RoleName) && a.IsDelete == false).OrderByDescending(a => a.ID).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
                }
                else
                {
                    count = roleRepository.Entities.Where(a => a.IsDelete == false).Count();
                    return roleRepository.Entities.Where(a => a.IsDelete == false).OrderByDescending(a => a.ID).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(req.RoleName))
                {
                    count = roleRepository.Entities.Where(a => a.Name.Contains(req.RoleName) && a.IsDelete == false).Count();
                    return roleRepository.Entities.Where(a => a.Name.Contains(req.RoleName) && a.IsDelete == false).OrderByDescending(a => a.ID).ToList();
                }
                else
                {
                    count = roleRepository.Entities.Where(a => a.IsDelete == false).Count();
                    return roleRepository.Entities.Where(a => a.IsDelete == false).OrderByDescending(a => a.ID).ToList();
                }
            }
        }

        public bool AddRole(RoleInput role)
        {
            var info = roleRepository.Entities.FirstOrDefault(a => a.Name == role.Name);
            if (info != null)
            {
                throw new BizException("角色名重复!");
            }

            RoleAggregate roleAggregate = new RoleAggregate();
            roleAggregate.Name = role.Name;
            roleAggregate.RolePermissions = role.RolePermissions;
            roleAggregate.Status = role.Status;
            roleAggregate.IsDelete = false;
            roleAggregate.CT = DateTime.Now;
            roleAggregate.UT = DateTime.Now;

            roleRepository.Insert(roleAggregate);
            return efUnitOfWork.Commit() > 0;
        }

        public bool EditRole(RoleInput role)
        {
            RoleAggregate roleAggregate = new RoleAggregate();
            roleAggregate.ID = role.Id;
            roleAggregate.Name = role.Name;
            roleAggregate.RolePermissions = role.RolePermissions;
            roleAggregate.Status = role.Status;
            roleAggregate.IsDelete = false;
            roleAggregate.UT = DateTime.Now;

            roleRepository.Update(roleAggregate);

            return efUnitOfWork.Commit() > 0;
        }

        public bool DelRole(int id)
        {
            var info = roleRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (info == null) return false;

            info.IsDelete = true;
            roleRepository.Update(info);
            return efUnitOfWork.Commit() > 0;
        }


        #endregion

        #region 广告列表

        public List<BannerAggregate> GetBannerList(GetBannerListReq req, out int count)
        {
            if (req.Category != -1)
            {
                count = bannerRepository.Entities.Where(a => (int)a.Category == req.Category && a.IsDelete == false).Count();
                return bannerRepository.Entities.Where(a => (int)a.Category == req.Category && a.IsDelete == false).OrderBy(a => a.Sort).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = bannerRepository.Entities.Where(a => a.IsDelete == false).Count();
                return bannerRepository.Entities.Where(a => a.IsDelete == false).OrderBy(a => a.Sort).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }
        public bool AddBanner(BannerInput input)
        {
            BannerAggregate bannerAggregate = new BannerAggregate();
            bannerAggregate.Category = input.Category;
            bannerAggregate.Data = input.Data;
            bannerAggregate.Enable = input.Enable;
            bannerAggregate.ImgUrl = input.ImgUrl;
            bannerAggregate.Sort = input.Sort;
            bannerAggregate.IsDelete = false;
            bannerAggregate.EndTime = Convert.ToDateTime(input.EndTime);
            bannerAggregate.StartTime = Convert.ToDateTime(input.StartTime);
            bannerAggregate.CT = DateTime.Now;
            bannerAggregate.UT = DateTime.Now;

            bannerRepository.Insert(bannerAggregate);
            return efUnitOfWork.Commit() > 0;
        }

        public bool EditBanner(BannerInput input)
        {
            BannerAggregate bannerAggregate = new BannerAggregate();
            bannerAggregate.ID = input.Id;
            bannerAggregate.Category = input.Category;
            bannerAggregate.Data = input.Data;
            bannerAggregate.Enable = input.Enable;
            bannerAggregate.ImgUrl = input.ImgUrl;
            bannerAggregate.Sort = input.Sort;
            bannerAggregate.IsDelete = false;
            bannerAggregate.EndTime = Convert.ToDateTime(input.EndTime);
            bannerAggregate.StartTime = Convert.ToDateTime(input.StartTime);
            bannerAggregate.UT = DateTime.Now;

            bannerRepository.Update(bannerAggregate);
            return efUnitOfWork.Commit() > 0;
        }

        public bool DelBanner(int id)
        {
            var info = bannerRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (info == null) return false;
            info.IsDelete = true;
            bannerRepository.Update(info);
            return efUnitOfWork.Commit() > 0;
        }

        public List<GetCategorySelectReps> GetCategorySelect()
        {
            List<GetCategorySelectReps> list = new List<GetCategorySelectReps>();
            foreach (var item in Enum.GetValues(typeof(BannerCategoryEnum)))
            {
                GetCategorySelectReps info = new GetCategorySelectReps();
                info.Id = (int)item;
                info.CategoryName = item.ToString();
                list.Add(info);
            }
            return list;
        }

        #endregion

        #region 用户

        //用户列表
        public List<UserInfoAggregate> GetUserInfoList(GetUserInfoListReq req, out int count)
        {
            if (!string.IsNullOrEmpty(req.Phone) && !string.IsNullOrEmpty(req.InviteCode))
            {
                count = userRepository.Entities.Where(a => a.Phone == req.Phone && a.InviteCode == req.InviteCode).Count();
                return userRepository.Entities.Where(a => a.Phone == req.Phone && a.InviteCode == req.InviteCode).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else if (!string.IsNullOrEmpty(req.Phone))
            {
                count = userRepository.Entities.Where(a => a.Phone == req.Phone).Count();
                return userRepository.Entities.Where(a => a.Phone == req.Phone).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else if (!string.IsNullOrEmpty(req.InviteCode))
            {
                count = userRepository.Entities.Where(a => a.InviteCode == req.InviteCode).Count();
                return userRepository.Entities.Where(a => a.InviteCode == req.InviteCode).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = userRepository.Entities.Count();
                return userRepository.Entities.OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }

        //用户钱包 
        //收入/佣金 /保证金）
        public List<WalletAggregate> GetUserWalletList(GetUserWalletListReq req, out int count)
        {
            if (req.UserID != 0)
            {
                count = walletRepository.Entities.Where(a => a.UserID == req.UserID).Count();
                return walletRepository.Entities.Where(a => a.UserID == req.UserID).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = walletRepository.Entities.Count();
                return walletRepository.Entities.OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }

        //收入日志
        public List<InComeWalletLogAggregate> GetInComeWalletLogByWalletId(GetWalletLogByWalletIdReq req, out int count)
        {
            count = inComeWalletLogRepository.Entities.Where(a => a.WalletID == req.WalletId).Count();
            return inComeWalletLogRepository.Entities.Where(a => a.WalletID == req.WalletId).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
        }
        //充值记录
        public List<ChargeWalletLogAggregate> GetChargeWalletLogByWalletId(GetWalletLogByWalletIdReq req, out int count)
        {
            count = chargeWalletLogRepository.Entities.Where(a => a.WalletID == req.WalletId).Count();
            return chargeWalletLogRepository.Entities.Where(a => a.WalletID == req.WalletId).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
        }

        //黑名单
        public List<BlackListAggregate> GetBlackList(GetBlackListReq req, out int count)
        {
            if (req.UserID != 0)
            {
                count = blackListRepository.Entities.Where(a => a.UserID == req.UserID).Count();
                return blackListRepository.Entities.Where(a => a.UserID == req.UserID).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = blackListRepository.Entities.Count();
                return blackListRepository.Entities.OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }


        #endregion

        #region 提现管理

        public List<WithDrawAggregate> GetWithDrawList(GetWithDrawListReq req, out int count)
        {
            if (req.UserID != 0)
            {
                count = withDrawRepository.Entities.Where(a => a.UserID == req.UserID).Count();
                return withDrawRepository.Entities.Where(a => a.UserID == req.UserID).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = withDrawRepository.Entities.Count();
                return withDrawRepository.Entities.OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }

        public bool ExamineOkWithDraw(int id)
        {
            WithDrawAggregate info = withDrawRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (info != null && info.Status == WithDrawStatus.待审核)
            {
                info.AuditStatus = WithDrawAuditStatus.人审通过;
                info.Status = WithDrawStatus.打款成功;
                info.UT = DateTime.Now;

                info.AddDomainEvent(new FinishWithDrawEvent()
                {
                    ID = info.Identity,
                    UserID = info.UserID
                }); 

                withDrawRepository.Update(info);
                return efUnitOfWork.Commit() > 0;
            }
            else
            {
                throw new BizException("操作记录不存在");
            }
        }

        public bool ExamineErrorWithDraw(int id)
        {
            WithDrawAggregate info = withDrawRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (info != null && info.Status == WithDrawStatus.待审核)
            {

                info.Status = WithDrawStatus.打款失败;
                info.UT = DateTime.Now;
                try
                {
                    if (info.WalletType == WalletType.佣金钱包)
                    {
                        walletService.ChangeInComeWallet(info.UserID, info.Money, 0, -info.TaxFee, info.ID.ToString(),0, FinancialBizType.提现失败回退, "打款失败退回佣金钱包", "人工审核打款失败");
                    }
                    else if (info.WalletType == WalletType.充值钱包)
                    {
                        walletService.ChangeChargeWallet(info.UserID, info.Money, 0, info.ID.ToString(), FinancialBizType.提现失败回退, "打款失败退回充值钱包", "人工审核打款失败");
                    }
                }
                catch (Exception)
                {
                    throw new BizException("系统繁忙");
                }
                withDrawRepository.Update(info);
                return efUnitOfWork.Commit() > 0;
            }
            else
            {
                throw new BizException("操作记录不存在");
            }
        }

        #endregion

        #region 系统配置

        public List<DicAggregate> GetDicList(GetDicListReq req, out int count)
        {
            if (!string.IsNullOrEmpty(req.Key))
            {
                count = dicRepository.Entities.Where(a => a.KeyName == req.Key && a.IsDelete == false).Count();
                return dicRepository.Entities.Where(a => a.KeyName == req.Key && a.IsDelete == false).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = dicRepository.Entities.Where(a => a.IsDelete == false).Count();
                return dicRepository.Entities.Where(a => a.IsDelete == false).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }

        public bool AddDic(DicInput input)
        {
            DicAggregate aggregate = new DicAggregate();
            aggregate.GroupName = input.GroupName;
            aggregate.IsDelete = false;
            aggregate.KeyName = input.KeyName;
            aggregate.KeyValue = input.KeyValue;
            aggregate.CT = DateTime.Now;
            aggregate.UT = DateTime.Now;

            dicRepository.Insert(aggregate);
            return efUnitOfWork.Commit() > 0;
        }

        public bool EditDic(DicInput input)
        {
            DicAggregate aggregate = new DicAggregate();
            aggregate.ID = input.ID;
            aggregate.GroupName = input.GroupName;
            aggregate.IsDelete = false;
            aggregate.KeyName = input.KeyName;
            aggregate.KeyValue = input.KeyValue;
            aggregate.UT = DateTime.Now;

            dicRepository.Update(aggregate);
            return efUnitOfWork.Commit() > 0;
        }

        public bool DelDic(int id)
        {
            var info = dicRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (info == null) return false;
            info.IsDelete = true;
            dicRepository.Update(info);
            return efUnitOfWork.Commit() > 0;
        }


        #endregion

        #region 短信记录管理

        public List<SmsCodeAggregate> GetSmsCodeList(GetSmsCodeListReq req, out int count)
        {
            if (!string.IsNullOrEmpty(req.UserPhone))
            {
                count = smsCodeRepository.Entities.Where(a => a.Phone == req.UserPhone).Count();
                return smsCodeRepository.Entities.Where(a => a.Phone == req.UserPhone).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = smsCodeRepository.Entities.Count();
                return smsCodeRepository.Entities.OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }


        #endregion

        #region 财务记录管理

        public List<FinancialAggregate> GetFinancialList(GetFinancialListReq req, out int count)
        {
            if (req.UserID != 0)
            {
                count = financialRepository.Entities.Where(a => a.UserID == req.UserID).Count();
                return financialRepository.Entities.Where(a => a.UserID == req.UserID).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = financialRepository.Entities.Count();
                return financialRepository.Entities.OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }

        #endregion

        #region 系统消息

        public List<MessageAggregate> GetMessageList(GetMessageListReq req, out int count)
        {
            if (!string.IsNullOrEmpty(req.Body))
            {
                count = messageRepository.Entities.Where(a => a.Body.Contains(req.Body) && a.IsDelete == false).Count();
                return messageRepository.Entities.Where(a => a.Body.Contains(req.Body) && a.IsDelete == false).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
            else
            {
                count = messageRepository.Entities.Where(a => a.IsDelete == false).Count();
                return messageRepository.Entities.Where(a => a.IsDelete == false).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
            }
        }

        #endregion

        #region  客服问答管理

        public List<KFQAAggregate> GetKFQAAggregateList(GetKFQAAggregateListReq req, out int count)
        {
            count = kFQARepository.Entities.Where(a => a.IsDelete == false).Count();
            return kFQARepository.Entities.Where(a => a.IsDelete == false).OrderByDescending(a => a.CT).Skip((req.PageNum - 1) * req.PageSize).Take(req.PageSize).ToList();
        }

        public bool AddKFQA(KFQAInput input)
        {
            KFQAAggregate aggregate = new KFQAAggregate();
            aggregate.Answer = input.Answer;
            aggregate.Question = input.Question;
            aggregate.Sort = input.Sort;
            aggregate.CT = DateTime.Now;
            aggregate.UT = DateTime.Now;

            kFQARepository.Insert(aggregate);
            return efUnitOfWork.Commit() > 0;
        }

        public bool EditKFQA(KFQAInput input)
        {
            KFQAAggregate aggregate = new KFQAAggregate();
            aggregate.ID = input.ID;
            aggregate.Answer = input.Answer;
            aggregate.Question = input.Question;
            aggregate.Sort = input.Sort;
            aggregate.UT = DateTime.Now;

            kFQARepository.Update(aggregate);
            return efUnitOfWork.Commit() > 0;
        }

        public bool DelKFQA(int id)
        {
            var info = kFQARepository.Entities.FirstOrDefault(a => a.ID == id);
            if (info == null) return false;
            info.IsDelete = true;
            kFQARepository.Update(info);
            return efUnitOfWork.Commit() > 0;
        }

        #endregion



    }
}
